package org.asiainfo.flowable.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import lombok.RequiredArgsConstructor;
import org.asiainfo.common.core.domain.R;
import org.asiainfo.common.flowable.core.domain.ProcessQuery;
import org.asiainfo.common.log.annotation.Log;
import org.asiainfo.common.log.enums.BusinessType;
import org.asiainfo.common.mybatis.stream.core.page.PageQuery;
import org.asiainfo.common.mybatis.stream.core.page.TableDataInfo;
import org.asiainfo.common.web.core.BaseController;
import org.asiainfo.flowable.domain.vo.WfDeployVo;
import org.asiainfo.flowable.domain.vo.WfFormVo;
import org.asiainfo.flowable.service.WfDeployFormService;
import org.asiainfo.flowable.service.WfDeployService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 流程部署
 *
 * @author dotor-ww
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/wfDeploy")
@Validated
public class WfDeployController extends BaseController {

    private final WfDeployService wfDeployService;

    private final WfDeployFormService wfDeployFormService;

    /**
     * 查询流程部署列表
     *
     * @param processQuery 流程查询实体对象
     * @param pageQuery    分页
     * @return 流程部署列表
     */
    @SaCheckPermission("system:deploy:list")
    @GetMapping("/list")
    public TableDataInfo<WfDeployVo> list(ProcessQuery processQuery, PageQuery pageQuery) {
        return wfDeployService.queryPageList(processQuery, pageQuery);
    }

    /**
     * 查询流程部署版本列表
     *
     * @param processKey 流程定义key
     * @param pageQuery  分页
     * @return 流程部署版本列表
     */
    @SaCheckPermission("system:deploy:list")
    @GetMapping("/publishList")
    public TableDataInfo<WfDeployVo> publishList(String processKey, PageQuery pageQuery) {
        return wfDeployService.publishList(processKey, pageQuery);
    }

    /**
     * 查询流程部署xml
     *
     * @param definitionId 流程定义ID
     * @return 流程部署xml
     */
    @SaCheckPermission("system:deploy:query")
    @GetMapping("/bpmnXml/{definitionId}")
    public R<Object> getBpmnXml(@NotBlank(message = "流程定义ID不能为空") @PathVariable("definitionId") String definitionId) {
        return R.ok(wfDeployService.getBpmnXml(definitionId));
    }

    /**
     * 删除流程模型
     *
     * @param deployIds 流程部署ID集合
     * @return 删除结果
     */
    @SaCheckPermission("system:deploy:remove")
    @Log(title = "流程部署管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{deployIds}")
    public R<Void> remove(@NotEmpty(message = "流程部署ID不能为空") @PathVariable("deployIds") String[] deployIds) {
        wfDeployService.deleteByDeployIds(List.of(deployIds));
        return R.ok();
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param deployId 流程部署ID
     * @return 流程部署关联表单信息
     */
    @SaCheckPermission("system:deploy:query")
    @GetMapping("/form/{deployId}")
    public R<WfFormVo> queryFormByDeployId(@NotEmpty(message = "流程部署ID不能为空") @PathVariable("deployId") String deployId) {
        WfFormVo wfFormVo = wfDeployFormService.queryFormByDeployId(deployId);
        if (ObjectUtil.isNull(wfFormVo)) {
            return R.fail("请先配置表单信息");
        }
        return R.ok(wfFormVo);
    }

    /**
     * 激活/挂起流程
     *
     * @param state        状态(1:激活 2:挂起)
     * @param definitionId 流程定义ID
     * @return 激活/挂起结果
     */
    @SaCheckPermission("system:deploy:edit")
    @Log(title = "流程部署管理", businessType = BusinessType.UPDATE)
    @PutMapping("changeDeployState")
    public R<Void> changeDeployState(Integer state, String definitionId) {
        wfDeployService.changeDeployState(state, definitionId);
        return R.ok();
    }
}
